package com.imy.common.helper;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

public class PermissionHelper {
  public static final int REQUEST_OVERLAY = 4444;
  public static final int REQUEST_NOTIFY = 4445;
  public static String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE };

  public static boolean requestOverlayPermission(Activity context) {
    if (Build.VERSION.SDK_INT >= 23) {
      if (!Settings.canDrawOverlays(context)) {
        //Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
        //        Uri.parse("package:" + context.getPackageName()));
        //context.startActivityForResult(intent, REQUEST_OVERLAY);
        return false;
      } else {
        //有权限
        return true;
      }
    }
    return true;
  }

  public static boolean checkStoragePermission(Activity activity) {
    int i = ContextCompat.checkSelfPermission(activity, permissions[0]);
    if (i != PackageManager.PERMISSION_GRANTED) {
      return false;
    } else {
      return true;
    }
  }

  public static void jumpNotification(Activity context) {
    Intent localIntent = new Intent();
    //直接跳转到应用通知设置的代码：
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0及以上
      localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
      localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0以上到8.0以下
      localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
      localIntent.putExtra("app_package", context.getPackageName());
      localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
    }
    context.startActivityForResult(localIntent,REQUEST_NOTIFY);
  }

  public static void openFloating(Activity context){
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
        Uri.parse("package:" + context.getPackageName()));
    context.startActivityForResult(intent, PermissionHelper.REQUEST_OVERLAY);
  }
  public static boolean isNotificationEnabled(Context context) {
   return NotificationManagerCompat.from(context).areNotificationsEnabled();
  }
  /**
   * 跳转到通知权限设置界面
   */
  public static void openNotificationSetting(Context context) {
    Intent intent = new Intent();
    try {

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
        intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
      } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
        intent.putExtra("app_package", context.getPackageName());
        intent.putExtra("app_uid", context.getApplicationInfo().uid);
      } else {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", context.getPackageName(), null));
      }
    } catch (Exception e) {
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
      intent.setData(Uri.fromParts("package", context.getPackageName(), null));
    }
    // 判断是否含有intent的目标activity，有的话再跳转
    if (intent.resolveActivity(context.getPackageManager()) != null) {
      context.startActivity(intent);
    }
  }
}
